using Game.Core;
using Game.Main.Model;
using Game.Manager;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class ThermometerBItem : BaseItem
	{
		private enum Anim
		{
			Static = 0,
			Change = 1
		}

		public float fillAmount;

		protected SpriteRenderer waterSprite;

		private Anim anim;

		private float changeTimer;

		private float animTimer;

		private bool isAdd;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			waterSprite = model.Find("WaterFill").GetComponent<SpriteRenderer>();
			float num = waterSprite.sprite.texture.height;
			Rect textureRect = waterSprite.sprite.textureRect;
			float num2 = textureRect.yMin / num;
			float value = textureRect.yMax / num - num2;
			waterSprite.material.SetFloat("_StartY", num2);
			waterSprite.material.SetFloat("_TotalY", value);
			SetFillAmount(data.floatParam1 + 0.5f);
		}

		protected override void LateUpdate()
		{
			base.LateUpdate();
			if (anim != Anim.Change || !(animTimer < 2f))
			{
				return;
			}
			animTimer += Time.deltaTime;
			if (changeTimer > 0f)
			{
				changeTimer -= Time.deltaTime;
				if (isAdd)
				{
					SetFillAmount(fillAmount + Time.deltaTime / 2f);
				}
				else
				{
					SetFillAmount(fillAmount - Time.deltaTime / 2f);
				}
				if (changeTimer <= 0f || fillAmount == 1f || fillAmount == 0f)
				{
					changeTimer = Random.Range(0.3f, 0.6f);
					isAdd = !isAdd;
				}
			}
			if (animTimer >= 2f)
			{
				anim = Anim.Static;
				animTimer = 0f;
			}
		}

		protected void SetFillAmount(float val)
		{
			if (val > 1f)
			{
				val = 1f;
			}
			else if (val < 0f)
			{
				val = 0f;
			}
			fillAmount = val;
			waterSprite.material.SetFloat("_Fill", fillAmount);
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["62"] = fillAmount - 0.5f;
		}

		public override void QuicklyTouch(TouchEventData touchEventData)
		{
			base.QuicklyTouch(touchEventData);
			Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
			if (anim == Anim.Static)
			{
				anim = Anim.Change;
				animTimer = 0f;
				changeTimer = Random.Range(0.3f, 0.6f);
				isAdd = Random.Range(0, 2) == 0;
			}
		}
	}
}
